const path = require("path");
const Koa = require("koa");
const parameter = require('koa-parameter');
const { koaBody } = require("koa-body");
const KoaStatic = require("koa-static");
const cors = require("cors");
console.log(process.cwd(), "--cwd进程目录");

console.log(path.basename("./errorhandler.js"), "path.basename");
const router = require("../routes/index.js");
let app = new Koa();
// app.use(cors)
parameter(app);
app.use(
  koaBody({
    multipart: true,
    formidable: {
      //上传到哪个文件
      //在配置选项option里，不推荐使用相对路径
      //在option里的相对路径，不是相对的当前文件，相对process.cws()
      uploadDir: path.join(__dirname, "../uploads"),
      //是否保留文件扩展名
      keepExtensions: true,
    },
  })
);

app.use(KoaStatic(path.join(__dirname, "../uploads")));
app.use(router.routes()).use(router.allowedMethods());

module.exports = app;
